﻿using System;
using System.Threading;

namespace Yoozoo.Modules.Download
{
    public class CheckResourceThread
    {
        public enum StateType
        {
            Idle,
            Running,
        }

        public StateType State = StateType.Idle;

        private Thread thread;

        public void Start(Action action)
        {
            if (State == StateType.Running)
                return;

            Exit();
            thread = new Thread(() =>
            {
                action?.Invoke();
                State = StateType.Idle;
                thread = null;
            });
            State = StateType.Running;
            thread.Start();
        }

        public void Exit()
        {
            if (thread != null)
            {
                thread.Abort();
                thread = null;
            }
            
            State = StateType.Idle;
        }
    }
}
